﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ENG.TeachersDiary
{
  public partial class FrmOptions : Form
  {
    public FrmOptions()
    {
      InitializeComponent();
    }

    private void btnOpenDB_Click(object sender, EventArgs e)
    {
      ofdSdf.ShowDialog();

      string fn = ofdSdf.FileName;

      if (string.IsNullOrEmpty(fn))
        return;

      if (!System.IO.File.Exists(fn))
      {
        var res = MessageBox.Show(Properties.Resources.DatabaseDoesNotExistCreateNew,
          Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

        switch (res)
        {
          case System.Windows.Forms.DialogResult.Cancel:
            break;
          case System.Windows.Forms.DialogResult.No:
            Properties.Settings.Default.DBFile = fn;
            break;
          case System.Windows.Forms.DialogResult.Yes:
            try
            {
              Common.CreateNewDB(fn);
              Properties.Settings.Default.DBFile = fn;
            }
            catch (Exception ex)
            {
              MessageBox.Show(Properties.Resources.DatabaseCreationFailed + " " + ex.Message,
                Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            break;
          default:
            throw new NotSupportedException();
        }
      }
      else
        Properties.Settings.Default.DBFile = fn;
    }

    private void FrmOptions_FormClosing(object sender, FormClosingEventArgs e)
    {
      var r = MessageBox.Show(
        Properties.Resources.ApplyChangesQuestion
        , Application.ProductName,
        MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
      switch (r)
      {
        case System.Windows.Forms.DialogResult.Cancel:
          e.Cancel = true;
          break;
        case System.Windows.Forms.DialogResult.Yes:
          GetFromForm();
          Properties.Settings.Default.Save();
          break;
        case System.Windows.Forms.DialogResult.No:
          Properties.Settings.Default.Reload();
          break;
      }
    }

    private void btnOpenCss_Click(object sender, EventArgs e)
    {
      ofdCss.ShowDialog();

      var fn = ofdCss.FileName;

      if (!string.IsNullOrEmpty(fn))
      {
        Properties.Settings.Default.CssStyleFile = fn;
      }
    }

    private void FrmOptions_Load(object sender, EventArgs e)
    {
      ofdSdf.FileName = txtDb.Text;
      SetToForm();
    }

    private void SetToForm ()
    {
      // txtNumericFormat.Text = Properties.Settings.Default.NumberFormat;
      cmbDeleteDeleteFirst.SelectedIndex = Properties.Settings.Default.DeleteKeyDeletesFirst ? 0 : 1;
      cmbBackspaceDeleteFirst.SelectedIndex = Properties.Settings.Default.BackspaceKeyDeletesFirst ? 0 : 1;
    }

    private void GetFromForm()
    {
      var s = Properties.Settings.Default;

      // s.NumberFormat = txtNumericFormat.Text;
      s.DeleteKeyDeletesFirst = (cmbDeleteDeleteFirst.SelectedIndex == 0);
      s.BackspaceKeyDeletesFirst = (cmbBackspaceDeleteFirst.SelectedIndex == 0);
    }
  }
}
